﻿using System;
using System.Collections.Generic;
using System.IO;

namespace Compiler
{
    /// <summary>
    /// 四元式
    /// </summary>
    public class Quaternion
    {
        public Quaternion() { }

        public Quaternion(string op, string arg1, string arg2, string result)
        {
            Operator = op;
            Argument1 = arg1;
            Argument2 = arg2;
            Result = result;
        }
        /// <summary>
        /// 操作符
        /// </summary>
        public string Operator;
        /// <summary>
        /// 参数1
        /// </summary>
        public string Argument1;
        /// <summary>
        /// 参数2
        /// </summary>
        public string Argument2;
        /// <summary>
        /// 结果
        /// </summary>
        public string Result;
        /// <summary>
        /// 四元式序列
        /// </summary>
        public static List<Quaternion> Quaternions { get; set; } = new List<Quaternion>();
        /// <summary>
        /// 将四元式转成字符串
        /// </summary>
        public override string ToString() => $" ( {Operator} , {Argument1} , {Argument2} , {Result} )";
        /// <summary>
        /// 打印四元式
        /// </summary>
        public static void PrintQuaternions()
        {
            try
            {
                using (StreamWriter writer = new StreamWriter("../../Files/Quaternions.txt"))
                {
                    lock (Quaternions)
                    {
                        foreach (Quaternion q in Quaternions)
                        {
                            Compiler.WriteLine(q.ToString());
                            writer.WriteLine(q.ToString());
                        }
                    }
                }
            }
            catch (Exception e)
            {
                Compiler.Log(e);
            }
        }
    }
}
